#coding=utf-8
import os
import httplib2,urllib2,time
from BeautifulSoup import BeautifulSoup

from sdsource.web.exception import NoWebPageError,NoFileError

home = os.environ['HOME']
def get_soup(url, is_cached=False, encoding='gb18030', try_times=10, timeout=15):
    if is_cached:
        h = httplib2.Http("%s/stcache"%home, timeout=timeout)
    else:
    
        h = httplib2.Http(timeout=timeout)
        
    for i in range(try_times):
        try:
            resp, content = h.request(url)
        except Exception, e:
            print e
        else:
            if resp.status != 200:
                print 'status:',resp.status
                continue
            
            if resp['content-type'] and not resp['content-type'].startswith('text'): 
                print 'content-type:',resp['content-type']
                continue
            
            htmltext = content.decode(encoding, 'ignore').encode('u8')
            s = BeautifulSoup(htmltext, fromEncoding='u8')
            return s
    
    raise NoWebPageError(url)

def get_file(url, fpath, try_time=10, timeout=10):
    #bug:有可能下载到显示出错信息html页面,而不是所需要的file;
    
    import socket
    socket.setdefaulttimeout(timeout)
    
    i = 0
    while i < try_time:
        try:
            h = urllib2.urlopen(url)
            data = h.read()
            open(fpath, 'wb').write(data)
            return 
        except (socket.timeout,IOError), e:
            print url, e
            time.sleep(0.1)
        
        i = i + 1
    
    raise NoFileError(url) 

def get_jd(month):
    return (month-1)/3 + 1
    
def get_yjs(sy, sjd, ey, ejd):
    #jd 代表季度
    #eg: get_yjs(2005,2) ==> [[]]
    #注意:all params are integer
    
    l = []
    if sy == ey:
        for jd in range(sjd, ejd+1):
            l.append([sy, jd])
        return l
        
    for jd in range(sjd, 5):
        l.append( [sy, jd] )
        
    years = range(sy+1, ey)    
    for y in years:
        for jd in range(1, 5):
            l.append( [y, jd] )
    
    for jd in range(1, ejd+1):
        l.append( [ey, jd] )
    
    l = [[str(i[0]), str(i[1])] for i in l]
    return l
